package com.todo.delicacy.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.delicacy.application.base.apollo.FoodApollo;
import com.todo.delicacy.application.convert.FoodConvert;
import com.todo.delicacy.application.entity.bo.RestaurantDetailBO;
import com.todo.delicacy.application.entity.bo.RestaurantInfoBO;
import com.todo.delicacy.application.entity.condition.RestaurantInfoConditionBO;
import com.todo.delicacy.application.frontend.service.RestaurantInfoAggService;
import com.todo.delicacy.application.service.RestaurantInfoService;
import com.todo.delicacy.iface.frontend.request.QueryRestaurantByNameParam;
import com.todo.delicacy.iface.frontend.request.QueryRestaurantByPageParam;
import com.todo.delicacy.iface.frontend.request.QueryRestaurantDetailParam;
import com.todo.delicacy.iface.frontend.request.RecommendRestaurantByCityParam;
import com.todo.delicacy.iface.frontend.response.RestaurantBaseVO;
import com.todo.delicacy.iface.frontend.response.RestaurantDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RestaurantInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:48
 */
@Slf4j
@Service("restaurantInfoAggService")
public class RestaurantInfoAggServiceImpl implements RestaurantInfoAggService {

    @Resource
    private RestaurantInfoService restaurantInfoService;
    @Resource
    private FoodApollo foodApollo;

    @Override
    public BizResponse<RestaurantDetailVO> queryRestaurantDetailById(QueryRestaurantDetailParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        RestaurantDetailBO restaurantDetailBO = restaurantInfoService.queryRestaurantDetailById(Long.valueOf(param.getRestaurantId()));
        return BizResponse.success(FoodConvert.convertToRestaurantDetailVO(restaurantDetailBO));
    }

    @Override
    public BizResponse<List<RestaurantBaseVO>> recommendRestaurantListByCity(RecommendRestaurantByCityParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<Long> restaurantIdList = foodApollo.queryRecommendRestaurantListByCity(param.getCity());
        log.info("RestaurantInfoAggService#recommendRestaurantListByCity 根据城市查询配置的推荐餐厅数据完成 城市：【{}】，结果：【{}】", param.getCity(), JSONObject.toJSONString(restaurantIdList));
        if (CollectionUtils.isEmpty(restaurantIdList)) {
            return BizResponse.success();
        }
        List<RestaurantInfoBO> restaurantInfoBOList = restaurantInfoService.queryRestaurantInfoByIdList(restaurantIdList);
        return BizResponse.success(FoodConvert.convertToRestaurantBaseVOList(restaurantInfoBOList));
    }

    @Override
    public BizPageResponse<RestaurantBaseVO> queryRestaurantByPage(QueryRestaurantByPageParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        RestaurantInfoConditionBO conditionBO = FoodConvert.convertToRestaurantInfoConditionBO(param);
        PageResult<RestaurantInfoBO> pageResult = restaurantInfoService.queryRestaurantInfoByCondition(conditionBO);
        return BizPageResponse.success(FoodConvert.convertToRestaurantBaseVOPage(pageResult));
    }

    @Override
    public BizResponse<List<RestaurantBaseVO>> queryRestaurantListByName(QueryRestaurantByNameParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<RestaurantInfoBO> restaurantInfoBOList = restaurantInfoService.queryRestaurantInfoByName(param.getName());
        return BizResponse.success(FoodConvert.convertToRestaurantBaseVOList(restaurantInfoBOList));
    }
}
